package RenderLayer;

import ControllerLayer.BattleController;
import ModelLayer.Battle;
import ModelLayer.BattleCharacter;
import ModelLayer.BattleEnemy;
import ModelLayer.Game;
import ModelLayer.Menu;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.Typeface;

import com.example.genericrpg_android.GameView;

public class BattleView extends View {
	Context activity;
	Game mModel;
	Battle battle;
	BattleController controller;
	Menu battleMenu;
	Menu targetSelectMenu;
	BattleCharacter[] battleCharacters;
	BattleEnemy[] battleEnemies;
	
	private Typeface font;
	
	public Bitmap menuArrow2;
	public Bitmap menuArrow3;
	public Bitmap menuArrow4;
	public Bitmap background;
	public Bitmap[] characterSprites;
	public Bitmap[] enemySprites;
	
	boolean menuAccess;
	boolean targetSelectAccess;
	
	Paint paint;
	
	public BattleView(Game model, BattleController controller, Context activity) {
		this.activity = activity;
		mModel = model;
		this.controller = controller;
		battleMenu = controller.getBattleMenu();
		
		battleCharacters = model.getBattleCharacters();
		
		menuAccess = false;
		
		paint = new Paint();
		
		try {
			font = Typeface.createFromAsset(activity.getAssets(), "fonts/PetMe128.ttf");
			paint.setTypeface(font);
			paint.setTextSize(24.0f);

			menuArrow2 = BitmapFactory.decodeStream(activity.getAssets().open("images/menuArrow2.png")); 
			menuArrow3 = BitmapFactory.decodeStream(activity.getAssets().open("images/menuArrow3.png"));
			menuArrow4 = BitmapFactory.decodeStream(activity.getAssets().open("images/menuArrow4.png"));
			background = BitmapFactory.decodeStream(activity.getAssets().open("images/battle_background.png"));
			
			characterSprites = new Bitmap[battleCharacters.length];
			characterSprites[0] = BitmapFactory.decodeStream(activity.getAssets().open("sprites/Warrior_Idle.png")); 
			characterSprites[1] = BitmapFactory.decodeStream(activity.getAssets().open("sprites/Mage_Idle.png"));
			characterSprites[2] = BitmapFactory.decodeStream(activity.getAssets().open("sprites/Thief_Idle.png"));
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void render(Canvas g, GameView root) {
		// TODO Auto-generated method stub
		g.drawBitmap(background, new Rect(0, 0, background.getWidth(), background.getHeight()), new Rect(0, 0, root.getWidth(), root.getHeight()), null);
		
		paint.setColor(Color.WHITE);
		paint.setStrokeWidth(20);
		g.drawRoundRect(new RectF(0, (2 * root.getHeight() / 3) - 3 - 5, (root.getWidth() / 3) - 10 + 5 + 5,
				((2 * root.getHeight() / 3) - 3) + (root.getHeight() / 3) - 3 + 5), 15, 15, paint);
		g.drawRoundRect(new RectF((root.getWidth() / 3) + 8 - 4, (2 * root.getHeight() / 3) - 3 - 5, ((root.getWidth() / 3) + 8) + ((2 * root.getWidth() / 3) - 14) + 5,
				((2 * root.getHeight() / 3) - 3) + (root.getHeight() / 3) - 3 + 5), 15, 15, paint);
		
		paint.setShader(new LinearGradient(0, 0, root.getWidth(), root.getHeight(), Color.BLUE,
				Color.BLACK, Shader.TileMode.MIRROR));
		g.drawRoundRect(new RectF(5, (2 * root.getHeight() / 3) - 3, (root.getWidth() / 3) - 10 + 5,
				((2 * root.getHeight() / 3) - 3) + (root.getHeight() / 3) - 3), 15, 15, paint);
		g.drawRoundRect(new RectF((root.getWidth() / 3) + 8, (2 * root.getHeight() / 3) - 3, ((root.getWidth() / 3) + 8) + ((2 * root.getWidth() / 3) - 14),
				((2 * root.getHeight() / 3) - 3) + (root.getHeight() / 3) - 3), 15, 15, paint);
		paint.setShader(null);
		
		//g.setPaint(null);
		
		paint.setColor(Color.WHITE);
		paint.setTextSize(12.0f);
		drawString(g, paint, "HP", (root.getWidth() / 3) + 20, (2 * root.getHeight() / 3) + 20, root.getWidth());
		drawString(g, paint, "MP", (root.getWidth() / 3) + 46 + (2 * (root.getWidth() - 100) / 9), (2 * root.getHeight() / 3) + 20, root.getWidth());
		drawString(g, paint, "Action", (root.getWidth() / 3) + 15 + (4 * (root.getWidth() - 100) / 9), (2 * root.getHeight() / 3) + 20, root.getWidth());
		
		int lineHeight = (int)paint.getFontSpacing();
		int yOffset = (2 * root.getHeight() / 3) + 40;
		for (int i = 0; i < battleCharacters.length; i++) {
			paint.setColor(Color.WHITE);
			paint.setTextSize(18.0f);
			drawString(g, paint, battleCharacters[i].getName(), 20,
					yOffset + 5, root.getWidth());
			
			paint.setTextSize(12.0f);
			drawString(g, paint, battleCharacters[i].getCurrentHealth() + "/" + battleCharacters[i].getMaxHealth(), (root.getWidth() / 3) + 20, yOffset, root.getWidth());
			drawString(g, paint, battleCharacters[i].getCurrentMana() + "/" + battleCharacters[i].getMaxMana(), (root.getWidth() / 3) + 46 + (2 * (root.getWidth() - 100) / 9), yOffset, root.getWidth());
			
			paint.setColor(Color.RED);
			g.drawRect(new Rect((root.getWidth() / 3) + 20, yOffset - lineHeight + 20, ((root.getWidth() / 3) + 20) + (2 * root.getWidth() / 9), (yOffset - lineHeight + 20) + 5), paint);			
			paint.setColor(Color.GREEN);
			g.drawRect(new Rect((root.getWidth() / 3) + 20, yOffset - lineHeight + 20, 
					((root.getWidth() / 3) + 20) + ((2 * (root.getWidth()) / 9) - ((2 * (root.getWidth()) / 9) - (((2 * (root.getWidth()) / 9) * battleCharacters[i].getCurrentHealth()) / battleCharacters[i].getMaxHealth()))), 
					(yOffset - lineHeight + 20) + 5), paint);
			
			paint.setColor(Color.GRAY);
			g.drawRect(new Rect((root.getWidth() / 3) + 46 + (2 * (root.getWidth() - 100) / 9), yOffset - lineHeight + 20, 
					(root.getWidth() / 3) + 46 + (2 * (root.getWidth() - 100) / 9) + ((root.getWidth() - 100) / 9), (yOffset - lineHeight + 20) + 5), paint);			
			paint.setColor(Color.CYAN);
			g.drawRect(new Rect((root.getWidth() / 3) + 46 + (2 * (root.getWidth() - 100) / 9), yOffset - lineHeight + 20, 
					((root.getWidth() / 3) + 46 + (2 * (root.getWidth() - 100) / 9)) + (((root.getWidth() - 100) / 9) - (((root.getWidth() - 100) / 9) - ((((root.getWidth() - 100) / 9) * battleCharacters[i].getCurrentMana()) / battleCharacters[i].getMaxMana()))), 
					(yOffset - lineHeight + 20) + 5), paint);

			paint.setColor(Color.GRAY);
			g.drawRect(new Rect((root.getWidth() / 3) + 15 + (4 * (root.getWidth() - 100) / 9), yOffset - lineHeight + 5, 
					((root.getWidth() / 3) + 15 + (4 * (root.getWidth() - 100) / 9)) + ((2 * (root.getWidth() - 100) / 9) + 10), (yOffset - lineHeight + 5) + 25), paint);
			
			paint.setColor(Color.DKGRAY);
			g.drawRect(new Rect((root.getWidth() / 3) + 20 + (4 * (root.getWidth() - 100) / 9), yOffset - lineHeight + 10, 
					((root.getWidth() / 3) + 20 + (4 * (root.getWidth() - 100) / 9)) + (2 * (root.getWidth() - 100) / 9), (yOffset - lineHeight + 10) + 15), paint);
			
			paint.setColor(Color.WHITE);
			g.drawRect(new Rect((root.getWidth() / 3) + 20 + (4 * (root.getWidth() - 100) / 9), yOffset - lineHeight + 10, 
					((root.getWidth() / 3) + 20 + (4 * (root.getWidth() - 100) / 9)) + ((2 * (root.getWidth() - 100) / 9) - ((2 * (root.getWidth() - 100) / 9) - (((2 * (root.getWidth() - 100) / 9) * battleCharacters[i].getCurrentActionBar()) / BattleCharacter.actionBarMax))), 
					(yOffset - lineHeight + 10) + 15), paint);

			yOffset += lineHeight + 35;
		}
		
		for (int i = 0; i < characterSprites.length; i++) {
			int x = (3 * root.getWidth() / 4) + (i * 40);
			int y = (root.getHeight() / 2) - ((characterSprites.length - (i + 1)) * 100) - 20;
			g.drawBitmap(characterSprites[i], new Rect(0, 0, characterSprites[i].getWidth(), characterSprites[i].getHeight()), new Rect(x, y, x + (2 * characterSprites[i].getWidth()), y + (2 * characterSprites[i].getHeight())), null);
		}
		
		
		for (int i = 0; i < enemySprites.length; i++) {
			if (!battleEnemies[i].isDead()) {
				int x = (root.getWidth() / 4) + (i * 30);
				int y = (root.getHeight() / 2) - ((enemySprites.length - (i + 1)) * 80) + 20;
				g.drawBitmap(enemySprites[i], new Rect(0, 0, enemySprites[i].getWidth(), enemySprites[i].getHeight()), new Rect(x, y, x + (3 * enemySprites[i].getWidth()), y + (3 * enemySprites[i].getHeight())), null);
			}
		}
		
		
		if (menuAccess || targetSelectAccess) {
			
			paint.setColor(Color.WHITE);
			g.drawRoundRect(new RectF((root.getWidth() / 6) - 11 - 5, (2 * root.getHeight() / 3) - 3 - 5, ((root.getWidth() / 6) - 11) + ((root.getWidth() / 6) + 6) + 5,
					((2 * root.getHeight() / 3) - 3) + ((root.getHeight() / 3) - 3) + 5), 15, 15, paint);
			
			paint.setShader(new LinearGradient(0, 0, root.getWidth(), root.getHeight(), Color.BLUE,
					Color.BLACK, Shader.TileMode.MIRROR));
			g.drawRoundRect(new RectF((root.getWidth() / 6) - 11, (2 * root.getHeight() / 3) - 3, ((root.getWidth() / 6) - 11) + ((root.getWidth() / 6) + 6),
					((2 * root.getHeight() / 3) - 3) + ((root.getHeight() / 3) - 3)), 15, 15, paint);
			paint.setShader(null);
			
			paint.setColor(Color.WHITE);
			paint.setTextSize(16.0f);
			lineHeight = (int)paint.getFontSpacing();
			yOffset = ((2 * root.getHeight()) / 3) + 30;
			for (int i = 0; i < battleMenu.getOptions().size(); i++) {
				drawString(g, paint, battleMenu.getOptions().get(i), (root.getWidth() / 3) - 115,
						yOffset, root.getWidth() / 3);
				if (battleMenu.getOptions().get(i).equals(battleMenu.getOption())) {
					g.drawBitmap(menuArrow2, new Rect(0, 0,
							menuArrow2.getWidth(),
							menuArrow2.getHeight()), new Rect((root.getWidth() / 3) - 135,
									yOffset - lineHeight,
									(root.getWidth() / 3) - 120, yOffset), null);
				}

				yOffset += lineHeight + 15;
			}
			
			int activeCharacter = controller.getAvailableAction().get(0);
			int x = (3 * root.getWidth() / 4) + (activeCharacter * 40);
			int y = (root.getHeight() / 2) - ((characterSprites.length - (activeCharacter + 1)) * 100) - 60;
			g.drawBitmap(menuArrow3, new Rect(0, 0, menuArrow3.getWidth(), menuArrow3.getHeight()), new Rect(x, y, x + (2 * menuArrow3.getWidth()), y + (2 * menuArrow3.getHeight())), null);
		}
		
		if (targetSelectAccess) {			
			for (int i = 0; i < targetSelectMenu.getOptions().size(); i++) {
				if (targetSelectMenu.getOptions().get(i).equals(targetSelectMenu.getOption())) {
					int enemySelected = Integer.parseInt(targetSelectMenu.getOption());
					int x = (root.getWidth() / 4) + (enemySelected * 30) + (4 * enemySprites[enemySelected].getWidth());
					int y = (root.getHeight() / 2) - ((enemySprites.length - (enemySelected + 1)) * 80) + (2 * enemySprites[enemySelected].getHeight());
					g.drawBitmap(menuArrow4, new Rect(0, 0, menuArrow4.getWidth(), menuArrow4.getHeight()), new Rect(x, y, x + (menuArrow4.getWidth()), y + (menuArrow4.getHeight())), null);
				}
			}
		}
		
	}
	
	public void drawString(Canvas g, Paint paint, String s, int x, int y, int width) {
		float lineHeight = paint.getFontSpacing();

		int xOffset = x;
		int yOffset = y;

		String[] words = s.split(" ");

		for (String word : words) {
			float wordWidth = paint.measureText(word + " ");

			if (xOffset + wordWidth >= x + width) {
				yOffset += lineHeight;
				xOffset = x;
			}

			g.drawText(word, xOffset, yOffset, paint);
			xOffset += wordWidth;
		}
	}
	
	public void startNewBattle(Battle battle, BattleEnemy[] battleEnemies) {
		this.battle = battle;
		this.battleEnemies = battleEnemies;
		
		try {

			enemySprites = new Bitmap[battleEnemies.length];
			for (int i = 0; i < enemySprites.length; i++)
				enemySprites[i] = BitmapFactory.decodeStream(activity.getAssets().open("sprites/slime.png"));
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void setMenuAccess(boolean menuAccess) {
		this.menuAccess = menuAccess;
	}
	
	public void setTargetSelectAccess(boolean targetSelectAccess) {
		this.targetSelectAccess = targetSelectAccess;
		
		if (targetSelectAccess = true)
			targetSelectMenu = controller.getTargetSelectMenu();
	}

}
